#coding=utf-8
from __future__ import with_statement

from uliweb import expose, decorators

def get_teams_list_view(c):
    from uliweb.utils.generic import ListView
    from uliweb.orm import get_model
    from uliweb import request
    from uliweb.core.html import Tag
    from uliweb import orm
    
    def abbr(value, obj):
        return str(Tag('a', value, href='/resource/teams/%d' % obj.id))
        
    pageno = int(request.GET.get('pageno', 1)) - 1
    
    Team = get_model('team')
    #query = None
#    query = Team.filter
#    if request.user.is_manager:
#        query = request.user.members.all()
    condition = None
#    if c.get('tx'):
#        condition = (team.c.tx == int(c['tx'])) & condition
    if c.get('kfzx'):
        condition = (Team.c.kfzx == int(c['kfzx'])) & condition
    if c.get('name'):
        condition = (Team.c.name.like(c['name'])) & condition
#    if c.get('department'):
#        condition = (team.c.department == c['department']) & condition
#    query = Team.filter()
    fields_convert_map = {'abbr':abbr}
    fields = ('abbr', 'name', 'kfzx',  'desc', 'logo')
    view =  ListView(Team, condition=condition, fields=fields,#query=query,
        rows_per_page=settings.get_var('PARA/ROWS_PER_PAGE', 10), pageno=pageno, 
        fields_convert_map=fields_convert_map, id='teams_table')
#    view.types_convert_map = {orm.BooleanProperty:boolean_convert}
    return view

@decorators.require_login
@expose('/resource/teams/list')
def resource_teams_list():
    from query import create_team_query

    query_view = create_team_query(url_for(resource_teams_list))
    c = query_view.run()

    view = get_teams_list_view(c)
    if 'data' in request.GET:
        result = view.run(head=False, body=True)
        return json(result)
    else:
        result = view.run(head=True, body=False)
        result.update({'query_form':query_view.form})
        return result
    
@expose('/resource/teams/data')
def resource_teams_data():
    result = view.run(head=False, body=True)#此处的view从何而来？
    return json(result)

@decorators.require_login
@expose('/resource/teams/add')
def resource_teams_add():
    from uliweb.utils.generic import AddView, ManyToManySelectField
    from uliweb.orm import get_model
#    from forms import UserForm, EditUserForm
    import generic.views as g_views
    from functools import partial
    
    get_url = g_views.get_url('/resource/teams', restful=True)
    
    Team = get_model('team')
    
    def pre_save(data):
        if has_role(request.user,'kfzx_gm'):
            data['kfzx'] = request.user._kfzx_
#            data['tx'] = request.user._tx_
#            data['department'] = request.user.department
    
#    def post_save(obj, data):
#        obj.set_password(settings.PARA.DEFAULT_PASSWORD)
#        obj.users_systems.update(*data['system'])
#        obj.observers_systems.update(*data['ob_system'])
        
    if has_role(request.user,'kfzx_gm') and request.user._kfzx_ or request.user.is_superuser:
        if has_role(request.user,'kfzx_gm') and request.user._kfzx_:
           fields = ('abbr',  'name', 'desc', 'logo')
        elif request.user.is_superuser:
            fields = ('abbr',  'name', 'desc', 'logo' , 'kfzx') 
        
#        layout = ('abbr',  'name', 'desc', 'logo')

        view = AddView(Team, url_for(resource_teams_list), fields=fields, pre_save=pre_save)

    else:
        flash('你不是超级用户或开发中心管理员，无法增加团队信息！')
        return redirect(url_for(resource_teams_list))
    return view.run()
    
@decorators.require_login
@expose('/resource/teams/<int:id>')
def resource_teams_view(id):
    from uliweb.utils.generic import DetailView
#    from uliweb import orm
    from uliweb.orm import get_model
    
    Team = get_model('team')
 
    fields = ('abbr',  'name', 'desc', 'logo')
    view = DetailView(Team, int(id), fields=fields)
    return view.run()
    
@decorators.require_login
@expose('/resource/teams/<int:id>/edit')
def resource_teams_edit(id):
    from uliweb.utils.generic import EditView, ManyToManySelectField
    from uliweb.orm import get_model, get_connection
#    from forms import EditUserForm, UserForm

    Team = get_model('team')
    team = Team.get(id)
    
    
    if (has_role(request.user,'kfzx_gm') and request.user._kfzx_==team._kfzx_) or request.user.is_superuser:
        fields = ('abbr',  'name', 'desc', 'logo')
        view = EditView(Team, condition=int(id), ok_url=url_for(resource_teams_view, id=id), fields=fields)
        return view.run()

    else:
        flash('你不是超级用户或开发中心管理员，无法编辑此团队信息！')
        return redirect(url_for(resource_teams_list))
    
@decorators.require_login
@expose('/resource/teams/<int:id>/delete')
def resource_teams_delete(id):
    from uliweb.utils.generic import DeleteView
    from uliweb.orm import get_model
    Team = get_model('team')
    team = Team.get(id)
    
    if (has_role(request.user,'kfzx_gm') and request.user._kfzx_==team._kfzx_) or request.user.is_superuser:
        view = DeleteView(Team, condition=int(id), ok_url=url_for(resource_teams_list))
        return view.run()
    else:
        flash('你不是超级用户或开发中心管理员，无法删除团队信息！')
        return redirect(url_for(resource_teams_view, id=int(id)))
    

        
